#include <iostream>
#include <vector>
using namespace std;

const int N = 1e6 + 5;

int q[N], hh, tt = -1;

int main()
{
    int n, k; cin >> n >> k;
    vector<int> arr(n); for(auto& e : arr) cin >> e;
    for(int i = 0; i < n; ++i)
    {
        if(tt >= hh && q[hh] <= i - k) ++hh;
        while(tt >= hh && arr[q[tt]] >= arr[i]) --tt;
        q[++tt] = i;
        if(i >= k - 1) cout << arr[q[hh]] << ' ';
    }
    cout << endl;
    hh = 0, tt = -1;
    for(int i = 0; i < n; ++i)
    {
        if(tt >= hh && q[hh] <= i - k) ++hh;
        while(tt >= hh && arr[q[tt]] <= arr[i]) --tt;
        q[++tt] = i;
        if(i >= k - 1) cout << arr[q[hh]] << ' ';
    }
    return 0;
}